package com.eulog.client.trump;

import java.util.ArrayList;
import java.util.List;

public class TrumpCard {
	public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE }
	public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }
	
	private final Rank rank;
	private final Suit suit;
	private TrumpCard(Rank rank, Suit suit) {
		this.rank = rank;
		this.suit = suit;
	}

	public Rank rank() { return rank; }
	public Suit suit() { return suit; }
	public String toString() { return toStringRank() + toStringSuit(); }
	
	public boolean isAce() { if(rank == Rank.ACE) return true; return false;}
	
	public String toStringRank() {
		String name = "";
		
		switch(rank) {
		case DEUCE:
			name = "2";
			break;
		case THREE:
			name = "3";
			break;
		case FOUR:
			name = "4";
			break;
		case FIVE:
			name = "5";
			break;
		case SIX:
			name = "6";
			break;
		case SEVEN:
			name = "7";
			break;
		case EIGHT:
			name = "8";
			break;
		case NINE:
			name = "9";
			break;
		case TEN:
			name = "10";
			break;
		case JACK:
			name = "J";
			break;
		case QUEEN:
			name = "Q";
			break;
		case KING:
			name = "K";
			break;
		case ACE:
			name = "A";
			break;
		}
		
		return name;
	}

	public String toStringSuit() {
		String name = "";
		switch(suit)
		{
		case CLUBS:
			name += "C";
			break;
		case DIAMONDS:
			name += "D";
			break;
		case HEARTS:
			name += "H";
			break;
		case SPADES:
			name += "S";
			break;
		}
		return name;
	}
	
	public static int rankIndex(Rank r) {
		int index;
		switch(r) {
		case DEUCE:
			index = 0;
			break;
		case THREE:
			index = 1;
			break;
		case FOUR:
			index = 2;
			break;
		case FIVE:
			index = 3;
			break;
		case SIX:
			index = 4;
			break;
		case SEVEN:
			index = 5;
			break;
		case EIGHT:
			index = 6;
			break;
		case NINE:
			index = 7;
			break;
		case TEN:
			index = 8;
			break;
		case JACK:
			index = 9;
			break;
		case QUEEN:
			index = 10;
			break;
		case KING:
			index = 11;
			break;
			
		default:
		case ACE:
			index = 12;
			break;
		}
		return index;
	}

	public static int suitIndex(Suit s) {
		int index;
		switch(s)
		{
		case CLUBS:
			index = 0;
			break;
		case DIAMONDS:
			index = 1;
			break;
		case HEARTS:
			index = 2;
			break;
			
		default:
		case SPADES:
			index = 3;
			break;
		}
		return index;
	}
	
	public static Rank getRankByValue(int value) {
		if(value == 1) value = 14;
		return protoDeck.get(value-2).rank;
	}
	
	private static final List<TrumpCard> protoDeck = new ArrayList<TrumpCard>();

	// Initialize prototype deck
	static {
		for (Suit suit : Suit.values())
			for (Rank rank : Rank.values())
				protoDeck.add(new TrumpCard(rank, suit));
	}
	
	public static TrumpCard getTrumpCard(Suit s,Rank r) {
		return protoDeck.get(suitIndex(s)*13+rankIndex(r));
	}

	
	public static ArrayList<TrumpCard> newDeck() {
		return new ArrayList<TrumpCard>(protoDeck); // Return copy of prototype deck
	}
}
